#include "friendlist.h"

Friendlist::Friendlist(QObject *parent) : QObject(parent)
{
}

FriendlistElement* Friendlist::operator[](int i)
{
    if( i >= m_friendlist.size() )
        return NULL;

    return &m_friendlist[i];
}

FriendlistElement* Friendlist::at(int i)
{
    if( i >= m_friendlist.size() )
        return NULL;

    return &m_friendlist[i];
}

int Friendlist::size()
{
    return m_friendlist.size();
}

void Friendlist::parseFromString(QString stringToParse)
{
    m_friendlist.resize(0);
    while( stringToParse.length() > 1 )
    {
        m_friendlist.resize(m_friendlist.size() + 1);
        // Extracting friends data from incomming friendlist
        QString myFriend  = stringToParse.left( stringToParse.indexOf(';') );

        QString loginName = myFriend.left( myFriend.indexOf(':') ); // Should popup as tooltip, unused up to now

        QString nickname  = myFriend.right( myFriend.length() - myFriend.indexOf(':') - 1 );
        nickname          = nickname.left( nickname.indexOf(':') );

        QString status    = myFriend.mid( myFriend.indexOf(':', myFriend.indexOf(':') + 1) + 1 );
        bool online       = ( status == "online" ? true : false );

        m_friendlist.at(m_friendlist.size() - 1).loginName = loginName;
        m_friendlist.at(m_friendlist.size() - 1).nickname  = nickname;
        m_friendlist.at(m_friendlist.size() - 1).status    = status;
        m_friendlist.at(m_friendlist.size() - 1).online    = online;
        // Remove friends data from incomming friendlist
        stringToParse = stringToParse.right( stringToParse.length() - stringToParse.indexOf(';') - 1 );
    }
}
